import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

public class Consumer {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.121.129:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "aaaa");
        //同一group_id 的不同消费者只要一个消费完了所有数据其他消费者将读取不了数据
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        KafkaConsumer<String, String> kfkConsumer = new KafkaConsumer<String, String>(properties);
        kfkConsumer.subscribe(Arrays.asList("supermarket"));//同一topic可以由不同消费者同时消费数据
        while (true) {
            ConsumerRecords<String, String> conRecord = kfkConsumer.poll(1);
            for (ConsumerRecord<String, String> consumer : conRecord) {
                System.out.println("value is " + consumer.value());
            }


        }

    }
}